package com.compomics.rover.gui;

import org.apache.log4j.Logger;

import com.compomics.rover.general.singelton.QuantitativeValidationSingelton;
import com.compomics.rover.general.quantitation.ReferenceSet;

import javax.swing.*;
import java.awt.*;
import java.util.HashMap;

/**
 * Created by IntelliJ IDEA.
 * User: niklaas
 * Date: 22-Apr-2009
 * Time: 10:34:38
 */
public class ReferenceSetInfoFrame extends JFrame {
    // Class specific log4j logger for ReferenceSetInfoFrame instances.
    private static Logger logger = Logger.getLogger(ReferenceSetInfoFrame.class);

    /**
     * This distiller validation singelton holds information for the calculation of the ratio
     */
    private QuantitativeValidationSingelton iQuantitativeValidationSingelton = QuantitativeValidationSingelton.getInstance();
    /**
     * The reference set
     */
    private ReferenceSet iReferenceSet;
    private JLabel lblProteinsNumber;
    private JPanel panelInfo;
    private JPanel jpanContent;


    public ReferenceSetInfoFrame() {
        super("Reference set info");
        //setup ui
        $$$setupUI$$$();
        //get the reference set
        iReferenceSet = iQuantitativeValidationSingelton.getReferenceSet();
        //set the number of used proteins
        lblProteinsNumber.setText(String.valueOf(iReferenceSet.getUsedProteinsNumber()));
        //create the panel to hold the rest of the information
        panelInfo.setLayout(new BoxLayout(panelInfo, BoxLayout.Y_AXIS));
        //get the types
        String[] lTypes = iReferenceSet.getTypes();
        for (int i = 0; i < lTypes.length; i++) {
            panelInfo.add(new JLabel("  -  Type: " + lTypes[i] + "         Ratios used: " + iReferenceSet.getUsedRatiosByType()[i]));
            panelInfo.add(Box.createVerticalStrut(5));
            panelInfo.add(new JLabel("         Real distribution [2.5%; 97.5%] = [" + Math.round(iReferenceSet.getPercentileReal(lTypes[i], 2.5) * 1000.0) / 1000.0 + "; " + Math.round(iReferenceSet.getPercentileReal(lTypes[i], 97.5) * 1000.0) / 1000.0 + "]"));
            panelInfo.add(Box.createVerticalStrut(5));
            panelInfo.add(new JLabel("         Huber estimated distribution [-1.96; 1.96] = [" + Math.round(iReferenceSet.getPercentileHuber(lTypes[i], -1.96) * 1000.0) / 1000.0 + "; " + Math.round(iReferenceSet.getPercentileHuber(lTypes[i], 1.96) * 1000.0) / 1000.0 + "]"));
            panelInfo.add(Box.createVerticalStrut(5));
            panelInfo.add(new JLabel("                     Standard deviation (log2) = " + Math.round((Double) (iReferenceSet.getHuberEstimatorsForType(lTypes[i]).get("stdev")) * 10000.0) / 10000.0));
            panelInfo.add(Box.createVerticalStrut(5));
            panelInfo.add(new JLabel("                     Mean (log2) = " + Math.round((Double) (iReferenceSet.getHuberEstimatorsForType(lTypes[i]).get("mean")) * 10000.0) / 10000.0));
            panelInfo.add(Box.createVerticalStrut(5));


            if (iQuantitativeValidationSingelton.getTitles() != null) {
                if (iQuantitativeValidationSingelton.getTitles().size() > 1) {
                    for (int j = 0; j < iQuantitativeValidationSingelton.getTitles().size(); j++) {
                        HashMap lMap = iReferenceSet.getHuberEstimatorsForDifferentSets().get(j)[i];
                        HashMap lMapOriginal = iReferenceSet.getHuberEstimatorsForDifferentSetsOriginal().get(j)[i];
                        panelInfo.add(new JLabel("    -    Set: " + iQuantitativeValidationSingelton.getTitles().get(j) + "         Ratios used: " + lMap.get("numberofratios")));
                        panelInfo.add(Box.createVerticalStrut(5));
                        panelInfo.add(new JLabel("                     Standard deviation (log2) = " + Math.round((Double) (lMap.get("stdev")) * 10000.0) / 10000.0));
                        panelInfo.add(Box.createVerticalStrut(5));
                        panelInfo.add(new JLabel("                     Mean (log2) = " + Math.round((Double) (lMap.get("mean")) * 10000.0) / 10000.0));
                        panelInfo.add(Box.createVerticalStrut(5));
                        panelInfo.add(new JLabel("                     Standard deviation Original (log2) = " + Math.round((Double) (lMapOriginal.get("stdev")) * 10000.0) / 10000.0));
                        panelInfo.add(Box.createVerticalStrut(5));
                        panelInfo.add(new JLabel("                     Mean Original(log2) = " + Math.round((Double) (lMapOriginal.get("mean")) * 10000.0) / 10000.0));
                        panelInfo.add(Box.createVerticalStrut(5));

                    }
                }
            }


        }


        this.setContentPane(jpanContent);
        this.pack();
        this.setLocation(400, 400);
        this.setVisible(true);


    }


    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        jpanContent = new JPanel();
        jpanContent.setLayout(new GridBagLayout());
        final JLabel label1 = new JLabel();
        label1.setText("Used proteins in reference set: ");
        GridBagConstraints gbc;
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5, 5, 5, 5);
        jpanContent.add(label1, gbc);
        lblProteinsNumber = new JLabel();
        lblProteinsNumber.setText("#");
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5, 5, 5, 5);
        jpanContent.add(lblProteinsNumber, gbc);
        panelInfo = new JPanel();
        panelInfo.setLayout(new GridBagLayout());
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 2;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(5, 5, 5, 5);
        jpanContent.add(panelInfo, gbc);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return jpanContent;
    }
}
